/**
  ******************************************************************************
  * @file    USART/Polling/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and peripherals
  *          interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "loopbuf.h"
#include "hardware.h"
#include "IntTimeSensor.h"

/* all extern interrupt handler */
extern int USART_DMA_Tx_Interrupt_Handler(int port);
extern void USART_Rxd_Interrupt_Handler(int port, char c);
extern void tick_plus(void);
extern void ADC_DMA_Done_Callback(void);
extern void SensorChannelADCDMADoneCallback(void);
extern void ADC2EndOfConvertInterruptHandler(void);
extern void Timer_Callback(void);
extern void MMA8451DataReadyIntHandler(void);
extern void TIM2InputCaptureModeInterruptHandler(void);

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSV_Handler exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

//*****************************************************************************
//*		Sytemtick Interrupt
//*		For Systme Tick, period is 1 millisecond
//*****************************************************************************
void SysTick_Handler(void) {
	tick_plus();
}

//*****************************************************************************
//* 	EXTI0 Interrupt
//* 	For Channel3, IOB is EXTI_Line0
//*****************************************************************************
void EXTI0_IRQHandler(void) {
	if(EXTI_GetITStatus(EXTI_Line0) != RESET) {
		/* Clear the  EXTI line 0 pending bit */
		EXTI_ClearITPendingBit(EXTI_Line0);
		
		if(ExtIntCallbacks[2]) 
			ExtIntCallbacks[2](2, GPIO_ReadInputDataBit(CH3_IOB_PORT, CH3_IOB_PIN) ? 1 : 0);
	}	
}

//*****************************************************************************
//*		EXIT1 Interrupt
//* 	For Channel1, IOB is EXTI_Line1
//*****************************************************************************
void EXTI1_IRQHandler(void) {
	if(EXTI_GetITStatus(EXTI_Line1) != RESET) {
		/* Clear the  EXTI line 1 pending bit */
		EXTI_ClearITPendingBit(EXTI_Line1);
		
		if(ExtIntCallbacks[0]) 
			ExtIntCallbacks[0](0, GPIO_ReadInputDataBit(CH1_IOB_PORT, CH1_IOB_PIN) ? 1 : 0);
	}	
}

//*****************************************************************************
//*	 	EXTI2 Interrupt
//* 	For Channel2, IOB is EXTI_Line2
//*****************************************************************************
void EXTI2_IRQHandler(void) {
	if(EXTI_GetITStatus(EXTI_Line2) != RESET) {
		/* Clear the  EXTI line 0 pending bit */
		EXTI_ClearITPendingBit(EXTI_Line2);
		
		if(ExtIntCallbacks[1]) 
			ExtIntCallbacks[1](1, GPIO_ReadInputDataBit(CH2_IOB_PORT, CH2_IOB_PIN) ? 1 : 0);
	}	
}

//*****************************************************************************
//*		EXTI 9..5 Interrupt
//* 	For Keypad which is EXTI_Line8
//*****************************************************************************
void EXTI9_5_IRQHandler(void) {
	if(EXTI_GetITStatus(EXTI_Line8) != RESET) {
		EXTI_ClearITPendingBit(EXTI_Line8);
		
		
	} 
}

//*****************************************************************************
//*		EXTI 10..15 Interrupt
//*		For Gravity Sensor MMA8451 INT1 whici is EXTI_Line14
//*****************************************************************************
void EXTI15_10_IRQHandler(void) {
	if(EXTI_GetITStatus(EXTI_Line14) != RESET) {
		/* Clear the  EXTI line 14 pending bit */
		EXTI_ClearITPendingBit(EXTI_Line14);
		
		MMA8451DataReadyIntHandler();
	}	
}

//*****************************************************************************
//* 	DMA Channel1 Interrupt
//*		For ADC1, Sensor Channel Used
//*****************************************************************************
void DMA1_Channel1_IRQHandler(void) {
	if(DMA_GetITStatus(DMA1_IT_TC1)) {
		DMA_ClearITPendingBit(DMA1_IT_GL1);
		
		SensorChannelADCDMADoneCallback();
	}
}

//*****************************************************************************
//*		ADC1_2 interrupt
//*		For Battery Monitor Driver
//*****************************************************************************
void ADC1_2_IRQHandler(void) {
	if(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) != RESET) {
		ADC_ClearFlag(ADC2, ADC_FLAG_EOC);
		
		ADC2EndOfConvertInterruptHandler();
	}
}

//*****************************************************************************
//*		TIM4 global Interrupt 
//*		For timer.c, delay_us, 2us per tick count, 131ms period
//*****************************************************************************
void TIM4_IRQHandler(void) {
	if(TIM_GetITStatus(TIM4 , TIM_IT_Update) == SET) {
		TIM_ClearITPendingBit(TIM4 , TIM_FLAG_Update);
		Timer_Callback();
	}
}

//*****************************************************************************
//*			USART and their DMA and IRQ interrupt Handler 
//*****************************************************************************

void USART1_IRQHandler(void) {
	/* RXD interrupt */
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
		USART_Rxd_Interrupt_Handler(0, USART_ReceiveData(USART1));
	}
}

void USART2_IRQHandler(void) {
	/* RXD interrupt */
	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
		USART_Rxd_Interrupt_Handler(1, USART_ReceiveData(USART2));
	}
}

void USART3_IRQHandler(void) {
	/* RXD interrupt */
	if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
		USART_Rxd_Interrupt_Handler(2, USART_ReceiveData(USART3));
	}
}

void DMA1_Channel4_IRQHandler(void) {
	if(DMA_GetITStatus(DMA1_IT_TC4)) {
		DMA_ClearITPendingBit(DMA1_IT_GL4);
	}
	/* index = 1-1 */
	USART_DMA_Tx_Interrupt_Handler(0);
}

void DMA1_Channel7_IRQHandler(void) {
	if(DMA_GetITStatus(DMA1_IT_TC7)) {
		DMA_ClearITPendingBit(DMA1_IT_GL7);
	}
	/* index = 2-1 */
	USART_DMA_Tx_Interrupt_Handler(1);
}

void DMA1_Channel2_IRQHandler(void) {
	if(DMA_GetITStatus(DMA1_IT_TC2)) {
		DMA_ClearITPendingBit(DMA1_IT_GL2);
	}
	/* index = 3-1 */
	USART_DMA_Tx_Interrupt_Handler(2);
}

void DMA1_Channel5_IRQHandler(void) {
	if(DMA_GetITStatus(DMA1_IT_TC5)) {
		DMA_ClearITPendingBit(DMA1_IT_GL5);
	}
	/* index = 3-1 */
	USART_Rxd_Interrupt_Handler(0, USART_ReceiveData(USART1));
}

void DMA1_Channel6_IRQHandler(void) {
	if(DMA_GetITStatus(DMA1_IT_TC6)) {
		DMA_ClearITPendingBit(DMA1_IT_GL6);
	}
	/* index = 3-1 */
	USART_Rxd_Interrupt_Handler(1, USART_ReceiveData(USART2));
}

void DMA1_Channel3_IRQHandler(void) {
	if(DMA_GetITStatus(DMA1_IT_TC3)) {
		DMA_ClearITPendingBit(DMA1_IT_GL3);
	}
	/* index = 3-1 */
	USART_Rxd_Interrupt_Handler(2, USART_ReceiveData(USART3));
}


 


/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
